package com.book.notice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.book.shelves.entity.BookStore;
import com.book.shelves.service.BookStoreService;
import com.book.common.annotation.Log;
import com.book.common.controller.BaseController;
import com.book.common.entity.FebsResponse;
import com.book.notice.entity.Notice;
import com.book.notice.service.NoticeService;
import com.book.warehouse.entity.WarehouseBook;
import com.book.warehouse.service.WarehouseBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import java.util.*;

@Controller
public class NoticeController extends BaseController {
    @Autowired
    NoticeService noticeService;
    @Autowired
    BookStoreService bookStoreService;
    @Autowired
    WarehouseBookService warehouseBookService;

    @PostMapping("notice/{ids}")
    @ResponseBody
    public FebsResponse addNotice(@PathVariable String ids, Notice notice) {
        String[] attr = ids.split(",");
        List<String> asList = Arrays.asList(attr);
        List<BookStore> bookStoreList = bookStoreService.askBookStore(attr);
        List<WarehouseBook> warehouseBooks = (List<WarehouseBook>)warehouseBookService.listByIds(asList);
        for (int i = 0; i < bookStoreList.size(); i++) {
            if (notice.getMessage() == null) {
                notice.setMessage(bookStoreList.get(i).getBookname() + ",");
            } else {
                notice.setMessage(notice.getMessage() + bookStoreList.get(i).getBookname() + ",");
            }
        }
        for (int i = 0; i < warehouseBooks.size(); i++) {
            if (notice.getMessage() == null) {
                notice.setMessage(warehouseBooks.get(i).getBookname() + ",");
            } else {
                notice.setMessage(notice.getMessage() + warehouseBooks.get(i).getBookname() + ",");
            }
        }
        if ("warehouse".equals(getCurrentUser().getUsername())) {
            Notice serviceOne = noticeService.getOne(new QueryWrapper<Notice>().lambda()
                    .eq(Notice::getMessage, notice.getMessage()).eq(Notice::getStatus, "0").eq(Notice::getMessagestatus, "0"));
            notice.setMessagestatus("0");
            notice.setMessage((notice.getMessage()).substring(0, notice.getMessage().length() - 1));
            notice.setStatus("0");
            if (serviceOne == null) {
                noticeService.addNotice(notice);
            }
            warehouseBookService.update(new UpdateWrapper<WarehouseBook>().lambda()
                    .in(WarehouseBook::getBookid, asList)
                    .set(true, WarehouseBook::getWarningstatus, "2"));
        }
        if ("shelves".equals(getCurrentUser().getUsername())) {
            Notice serviceOne = noticeService.getOne(new QueryWrapper<Notice>().lambda()
                    .eq(Notice::getMessage, notice.getMessage()).eq(Notice::getStatus, "0").eq(Notice::getMessagestatus, "1"));
            notice.setMessagestatus("1");
            notice.setMessage((notice.getMessage()).substring(0, notice.getMessage().length() - 1));
            notice.setStatus("0");
            if (serviceOne == null) {
                noticeService.addNotice(notice);
            }
            bookStoreService.update(new UpdateWrapper<BookStore>().lambda()
                    .notIn(BookStore::getFlag, "1")
                    .in(BookStore::getId, asList)
                    .set(true, BookStore::getFlag, "2"));
        }
        return new FebsResponse().success();
    }

    @Log("查看库存提示消息")
    @GetMapping("notice")
    @ResponseBody
    public FebsResponse noticeView() {
        List<Notice> noticeList = noticeService.list(new QueryWrapper<Notice>().lambda().eq(Notice::getStatus, "0"));
        //list向set集合转换，也可以用流的方式，原理一样。
        Set<Notice> noticeSet = new HashSet<>(noticeList);
        return new FebsResponse().success().data(noticeSet);
    }

    @PostMapping("notice/update/{message}")
    @ResponseBody
    public FebsResponse getNotice(@PathVariable String message) {
        if ("warehouse".equals(getCurrentUser().getUsername())) {
            noticeService.update(new UpdateWrapper<Notice>().lambda()
                    .in(Notice::getStatus, "0").eq(Notice::getMessagestatus, "0")
                    .set(true, Notice::getStatus, "1"));
        }
        if ("shelves".equals(getCurrentUser().getUsername())) {
            noticeService.update(new UpdateWrapper<Notice>().lambda()
                    .in(Notice::getStatus, "0").eq(Notice::getMessagestatus, "1")
                    .set(true, Notice::getStatus, "1"));
        }
        return new FebsResponse().success();
    }
}